package com.yj.lottery_system.dao.mapper;

import com.yj.lottery_system.dao.dataObject.Encrypt;
import com.yj.lottery_system.dao.dataObject.UserDO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface  UserMapper {
    /**
     * 查询邮箱绑定人数
     * @param email 邮箱
     * @return 绑定人数
     */
    @Select("select count(*) from user where email = #{email}")
     int countByMail(@Param("email") String email);
    /**
     * 查询电话号绑定人数
     * @param phoneNumber 电话号码
     * @return 绑定人数
     */
    @Select("select count(*) from user where phone_number = #{phoneNumber}")
     int countByPhone(@Param("phoneNumber") Encrypt phoneNumber);

    /**
     * 插入数据
     * @param userDO
     */
    @Insert("insert into user (user_name, email, phone_number, password, identity)"+
            " values (#{userName},#{email},#{phoneNumber},#{password},#{identity})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    void insert(UserDO userDO);

    /**
     * 根据电话号码返回用户信息
     * @param phoneNumber
     * @return
     */
    @Select("select * from user where phone_number = #{phoneNumber}")
    UserDO selectPhoneNum(@Param("phoneNumber") Encrypt phoneNumber);
    /**
     * 根据邮箱返回用户信息
     * @param email
     * @return
     */
    @Select("select * from user where email = #{email}")
    UserDO selectByByMail(@Param("email") String email);

    /**
     * 根据 identity 身份信息 查询用户信息
     * @param identity
     * @return
     */
    @Select("<script> select * from user <if test=\"identity!=null\"> where identity = #{identity} </if> order by id desc </script>")
    List<UserDO> selectUserListByIdentity(String identity);

    /**
     * 根据前端传入的id列表查询列表中存在数据库的值
     * @param ids id列表
     * @return id列表中存在数据库的id值
     */
    @Select("<script> " +
            "select id from user  " +
            "where id in " +
            "<foreach item = 'item' collection='items' open='(' separator=',' close=')' > " +
            " #{item} " +
            "</foreach>" +
            "</script>")
    List<Long> selectExitsByIds(@Param("items") List<Long> ids);

    /**
     * 根据人员id列表，批量查询人员信息
     * @param ids
     * @return
     */
    @Select("<script> " +
            "select * from user  " +
            "where id in " +
            "<foreach item = 'item' collection='items' open='(' separator=',' close=')' > " +
            " #{item} " +
            "</foreach>" +
            "</script>")
    List<UserDO> batchSelectByIds(@Param("items") List<Long> ids);

}

